﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lists;
using System.Data.SqlClient;
using GeneralLibs;

namespace ReportGenerators.QuanLyKho
{
    public partial class FrmThamSoTonMatHang : BasicForms.FrmThamSoBaoCao
    {
        public FrmThamSoTonMatHang()
        {
            InitializeComponent();
        }
        protected override void PrepareDataForReport()
        {
            VatTuHangHoa vatTuHangHoa = DanhMucVatTuHangHoa.Instance.GetByID((Guid)cmbVatTuHangHoa.EditValue);

            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            DataSet ds = new DataSet();
            try
            {
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandTimeout = GeneralLibs.Config.TimeOut;
                con.Open();
                SqlDataAdapter da = null;

                cmd.CommandText = "Select_ThongTinDoanhNghiep";
                cmd.Parameters.Clear();
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "ThongTinDoanhNghiep");

                DataTable dtChiTietTheKho = new DataTable("ChiTietTonMatHang");
                dtChiTietTheKho.Columns.Add("KyBaoCao", Type.GetType("System.String"));
                dtChiTietTheKho.Columns.Add("MaVTHH", Type.GetType("System.String"));
                dtChiTietTheKho.Columns.Add("TenVTHH", Type.GetType("System.String"));
                dtChiTietTheKho.Columns.Add("DonViTinh", Type.GetType("System.String"));
                dtChiTietTheKho.Columns.Add("Ten", Type.GetType("System.String"));
                dtChiTietTheKho.Rows.Add(dtChiTietTheKho.NewRow());
                dtChiTietTheKho.Rows[0]["KyBaoCao"] = _KyBaoCao.Ten;
                dtChiTietTheKho.Rows[0]["MaVTHH"] = vatTuHangHoa.Ma;
                dtChiTietTheKho.Rows[0]["TenVTHH"] = vatTuHangHoa.Ten;
                dtChiTietTheKho.Rows[0]["DonViTinh"] = vatTuHangHoa.DonViTinh;
                //dtChiTietTheKho.Rows[0]["Ten"] = kho.Ten;
                ds.Tables.Add(dtChiTietTheKho);

                cmd.CommandText = "Select_TonMatHang";
                cmd.CommandType = CommandType.StoredProcedure;
                //cmd.Parameters.AddWithValue("@KhoID", kho.ID);
                cmd.Parameters.AddWithValue("@VatTuHangHoaID", vatTuHangHoa.ID);
                cmd.Parameters.AddWithValue("@NgayBatDau", _KyBaoCao.ThoiDiemDau);
                cmd.Parameters.AddWithValue("@NgayKetThuc", _KyBaoCao.ThoiDiemCuoi);
                da.Fill(ds, "KhoanMucTonMatHang");

                _Report = new ReportTemplates.QuanLyKho.TonMatHang.RptTonMatHang();
                _Report.SetDataSource(ds);

            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
            finally
            {
                con.Close();
            }
        }

        protected override void PrintReport()
        {
            if (_Report != null)
            {
                ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(_Report, "Tồn thuốc");
                frm.Show();
            }
        }

        private void FrmThamSoTonMatHang_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            cmbKyBaoCao.SelectedIndex = 19;
            #region cmbVatTuHangHoa
            DataView dvVatTuHangHoa = DanhMucVatTuHangHoa.GetAll().DataView;
            cmbVatTuHangHoa.DataSource = dvVatTuHangHoa;
            cmbVatTuHangHoa.DisplayColumn = "Ten";
            cmbVatTuHangHoa.ValueColumn = "ID";
            cmbVatTuHangHoa.AddVisibleColumn("Ma", "Mã");
            cmbVatTuHangHoa.AddVisibleColumn("Ten", "Tên");
            cmbVatTuHangHoa.Buttons[1].Visible = false;
            #endregion
        }
    }
}
